<?php


class AdminController extends Zend_Controller_Action {

         function _createThumbnail($sourcePath, $destPath, $imageOldName, $imageNewName, $w, $h, $q = "JPG") {
                //  echo "<br><h1>$sourcePath   *** $destPath </h1><br>   ";
                $thumb = new GdThumb($sourcePath . $imageOldName);
                $thumb->save($sourcePath . $imageNewName, $q);
                $thumb->resize($w, $h);
                $thumb->save($destPath . $imageNewName, $q);
                unlink($sourcePath . $imageOldName);
        }

        public function init() {
                $userdata = new Zend_Session_Namespace('login');
                if (isset($userdata->userObj)) {
                        if ($userdata->userObj[0]['role'] != "administrator") {
                                header('Location: /shopflowers/public/index/index');
                        }
                } else {
                        header('Location: /shopflowers/public/auth');
                }
                /* Initialize action controller here */
//$this->_helper->layout->disableLayout();
                $this->_helper->layout()->disableLayout();
        }

        public function categoryindexAction() {
                $category = new Application_Model_Category();
                $categories = $category->getAllCategories();
                $adapter = new Zend_Paginator_Adapter_Array($categories);
                $paging = new Zend_Paginator($adapter);
                $paging->setItemCountPerPage(5);
                $page = $this->_request->getParam('page', 1);
                $paging->setCurrentPageNumber($page);
                $this->view->data = $paging;
        }

        public function categoryeditAction() {
                $table = new Application_Model_Category();
                $result = $table->getCategories($this->getRequest()->getParam("id"));
                $this->view->result = $result;
                $form = new Application_Form_Category();
                $this->view->form = $form;
                if ($this->getRequest()->isPost()) {
                        if ($form->isValid($_POST)) {
                                $where = $table->getAdapter()->quoteInto('id = ?', $this->getRequest()->getParam("id"));
                                $table->update($form->getValues(), $where);
                                $this->_redirect('admin/categoryindex');
                        }
                }
        }

        public function categorydeleteAction() {
                $table = new Application_Model_Category();
                $where = $table->getAdapter()->quoteInto('id = ?', $this->getRequest()->getParam("id"));
                $table->delete($where);
                $this->_redirect('admin/categoryindex');
        }

        public function categorynewAction() {
                $form = new Application_Form_Category();
                $this->view->form = $form;
                if ($this->getRequest()->isPost()) {
                        if ($form->isValid($_POST)) {
                                $category = new Application_Model_Category();
                                $category->insert($form->getValues());
                                $this->_redirect('admin/categoryindex');
                        }
                }
                //$this->view->form = $form;
        }

        public function productindexAction() {
                $product = new Application_Model_Product();
                $products = $product->getAllProduct();
                $adapter = new Zend_Paginator_Adapter_Array($products);
                $paging = new Zend_Paginator($adapter);
                $paging->setItemCountPerPage(5);
                $page = $this->_request->getParam('page', 1);
                $paging->setCurrentPageNumber($page);
                $this->view->data = $paging;
        }

        public function userindexAction() {
                $user = new Application_Model_User();
                $users = $user->getAllUsers();
                $adapter = new Zend_Paginator_Adapter_Array($users);
                $paging = new Zend_Paginator($adapter);
                $paging->setItemCountPerPage(5);
                $page = $this->_request->getParam('page', 1);
                $paging->setCurrentPageNumber($page);
                $this->view->data = $paging;
        }

        public function ordersAction() {
                $db = Zend_Db_Table_Abstract::getDefaultAdapter();
                $uid = $db->select()->from('orderline');
                $this->view->orderline = $db->fetchAll($uid);
                //$user_id = $s[0]['id'];
                // action body
        }

        public function usernewAction() {
                $users = new Application_Model_User();
                $form = new Application_Form_User();
                $this->view->form = $form;
                if ($this->getRequest()->isPost()) {
                        if ($form->isValid($_POST)) {
                                $data = $form->getValues();
                                if ($users->checkUnique($data['username'])) { // username is unique?
                                        $this->view->errorMessage = "Name already taken. Please choose another one.";
                                        return;
                                }
                                $data['date_created'] = date("Y-m-d h:i:s");
                                $data['token'] = rand(100, 1000);
                                $data['token'] = sha1($data['token']);
                                $data['password'] = SHA1(strtok($data['password'], $data['token']));
                                $user = new Application_Model_User();
                                $user->insert($data);
                                $this->_redirect('admin/userindex');
                        }
                }
        }

        public function userdeleteAction() {
                $table = new Application_Model_User();
                $where = $table->getAdapter()->quoteInto('id = ?', $this->getRequest()->getParam("id"));
                $data = array('status' => 'disable');
                $table->update($data, $where);
                $this->_redirect('admin/userindex');
        }

        public function usereditAction() {
                $table = new Application_Model_User();
                $result = $table->getUser($this->getRequest()->getParam("id"));
                $this->view->result = $result;
                $form = new Application_Form_User();
                $this->view->form = $form;
                if ($this->getRequest()->isPost()) {
                        if ($form->isValid($_POST)) {
                                $data = $form->getValues();
                                $where = $table->getAdapter()->quoteInto('id = ?', $this->getRequest()->getParam("id"));
                                $table->update($data, $where);
                                $this->_redirect('admin/userindex');
                        }
                }
        }

        //mina
        //mina
        public function productnewAction() {
                $product = new Application_Model_Product();
                $request = $this->getRequest();
                $form = new Application_Form_Product();
                if ($request->isPost()) {
                        if ($form->isValid($request->getPost())) {
                                if ($form->image->isUploaded()) {
                                        $values = $form->getValues();
                                        //change image name
                                        $imageOldName = $values['image'];
                                        $imageNewName = time() . ".jpg";
                                        $values['image'] = $imageNewName;
                                        $product->insert($values);
                                        // echo "<br><h1>/product_image_large/$values[image]  *** </h1><br>   ";
                                        $this->_createThumbnail(APPLICATION_PATH . "/../public/product_image_large/", APPLICATION_PATH . "/../public/product_images/", $imageOldName, $imageNewName, 100, 100);
                                }
                        }
                }
                $this->view->form = $form;
        }

        public function producteditAction() {
                $request = $this->getRequest();
                $productObj = new Application_Model_Product();
                $product = $productObj->getProduct($request->getParam('id'));
                $form = new Application_Form_Product();
                $productData = $product->toArray();
                $form->populate($productData[0]);
                $form->removeElement('image');
                if ($request->isPost()) {
                        if ($form->isValid($request->getPost())) {
                                  $values = $form->getValues();
                                  $where = $productObj->getAdapter()->quoteInto('id = ?', $this->getRequest()->getParam("id"));
                                        $productObj->update( $values ,  $where  );
                        }
                }
                $this->view->form = $form;
        }
        public function productdeleteAction() {
                $table = new Application_Model_Product();
                $where = $table->getAdapter()->quoteInto('id = ?', $this->getRequest()->getParam("id"));
                $table->delete($where);
                $this->_redirect('admin/productindex');
        }

}

